//
//  HealthyFilesViewController.m
//  掌上宏力
//
//  Created by admin on 2017/4/26.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "HealthyFilesViewController.h"
#import "HealthyFilesCell.h"
#import "HealthyInfoModel.h"
#import "HealthyViewModel.h"
#import "HealthyDetailViewController.h"

//所有体检记录
NSString *const healthy_url = @"api/Account/GetMyMainExamined";

@interface HealthyFilesViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSArray *dataArray;
@property (nonatomic,strong) UILabel *titleLab;
@property (nonatomic,strong) UIView *headView;
@property (nonatomic,strong) HealthyViewModel *viewModel;

@end

@implementation HealthyFilesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"健康档案";
    self.view.backgroundColor = TB_COLOR;
    [SVProgressHUD showWithStatus:@"正在加载中..."];
    [self getDataArray];
}
-(void)createTableView{
   
    [self createUserInfo];
    
    [self createHeadView];

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, _headView.bottom, kScreenW, kScreenH-_headView.bottom-64) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.sectionHeaderHeight = 0;
    _tableView.sectionFooterHeight = 5;
    _tableView.contentInset = UIEdgeInsetsMake(-35, 0, 0, 0);

    [self.view addSubview:_tableView];
}
-(void)createHeadView{
    //分割线
    UIView *line = [Factory createViewWithBackgroundColor:[UIColor lightGrayColor] frame:CGRectMake(5, _titleLab.bottom, kScreenW-10, 1)];
    [self.view addSubview:line];

    
    CGFloat width = (kScreenW-155-6-6)*0.5;
    CGFloat height = 30;
    _headView = [[UIView alloc] initWithFrame:CGRectMake(0, line.bottom+5, kScreenW-25, height)];
    _headView.backgroundColor = TB_COLOR;
    [self.view addSubview:_headView];

    UILabel *healthyIdL = [self createlabelWithFrame:CGRectMake(0, 0, 65, height) text:@"体检号"];
    UIView *line1 = [self getLineWithFrame:CGRectMake(healthyIdL.right, 5, 1, height-10)];

    UIView *line2 = [self getLineWithFrame:CGRectMake(healthyIdL.right, 5, 1, height-10)];
    
    UILabel *dateLab = [self createlabelWithFrame:CGRectMake(healthyIdL.right+1, 1, width, height) text:@"体检日期"];
    UIView *line3 = [self getLineWithFrame:CGRectMake(dateLab.right, 5, 1, height-10)];
    
    UILabel *resultDateLab = [self createlabelWithFrame:CGRectMake(dateLab.right+1, 1, width, height) text:@"结果日期"];
    UIView *line4 = [self getLineWithFrame:CGRectMake(resultDateLab.right, 5, 1, height-10)];
    
    UILabel *operatorLab = [self createlabelWithFrame:CGRectMake(resultDateLab.right+1, 1, 65, height) text:@"操作人"];
    
    [_headView addSubview:healthyIdL];
    [_headView addSubview:dateLab];
    [_headView addSubview:resultDateLab];
    [_headView addSubview:operatorLab];
    
    [_headView addSubview:line1];
    [_headView addSubview:line2];
    [_headView addSubview:line3];
    [_headView addSubview:line4];
}
-(UIView *)getLineWithFrame:(CGRect)frame{
    UIView *line = [[UIView alloc] initWithFrame:frame];
    line.backgroundColor = [UIColor lightGrayColor];
    return line;
}
-(UILabel *)createlabelWithFrame:(CGRect)frame text:(NSString *)text{
    UILabel *lab = [Factory createLabelWithTitle:text frame:frame fontSize:15];
    lab.textAlignment = NSTextAlignmentCenter;
    lab.backgroundColor = TB_COLOR;
    lab.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
    return lab;
}
-(void)createUserInfo{
    User *user = [User currentUser];
    NSString *titleStr = [NSString stringWithFormat:@"患者ID号:%@  患者姓名:%@ ",user.patientId,user.username];
    NSRange range2 = [titleStr rangeOfString:user.patientId];
    NSRange range3 = [titleStr rangeOfString:user.username];
    NSDictionary *attribute = @{NSForegroundColorAttributeName:[UIColor blueColor] };
    NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc] initWithString:titleStr];

    [attriStr addAttributes:attribute range:range2];
    [attriStr addAttributes:attribute range:range3];
    
    _titleLab = [Factory createLabelWithFrame:CGRectMake(10, 0, kScreenW, 50) attributeText:attriStr font:15];
    _titleLab.textAlignment = NSTextAlignmentLeft;
    _titleLab.backgroundColor = TB_COLOR;
    [self.view addSubview:_titleLab];

}
-(void)getDataArray{
    
    _viewModel = [[HealthyViewModel alloc] init];
    [_viewModel getHealthyDatasSuccess:^(NSArray *array) {
        _dataArray = [NSArray arrayWithArray:array];
        if (array.count == 0) {
            [self performSelector:@selector(popToSuperView) withObject:nil afterDelay:1.0];
        }else{
            [self createTableView];
        }
        [SVProgressHUD dismiss];

    }];
}
-(void)popToSuperView{
    [self.navigationController popViewControllerAnimated:YES];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HealthyDetailViewController *resultVC = [[HealthyDetailViewController alloc] init];
    HealthyInfoModel *info = _dataArray[indexPath.section];
    resultVC.info = info;
    [self pushViewControllerTo:resultVC];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *resuedId = @"cellId";
    HealthyFilesCell *cell = [tableView dequeueReusableCellWithIdentifier:resuedId];
    if (!cell) {
        cell = [[HealthyFilesCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:resuedId];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    HealthyInfoModel *model = _dataArray[indexPath.section];
    [cell setHealthyInfoWithModel:model];
    return cell;
}


@end
